import time

from flask.ext.cache import Cache
from flask_wechat.qyh import Qyh


cache = Cache()
qyh = Qyh()


QYH_TOKEN = "wechat-token"


@qyh.token_setter
def set_wx_token(token, expires):
    timeout = int(expires - time.time())
    cache.set(QYH_TOKEN, (token, expires), timeout)


@qyh.token_getter
def get_wx_token():
    cached = cache.get(QYH_TOKEN)
    return (None, None) if cached is None else cached
